﻿using IM.Easy.AuthorizationServices.IServices;
using IM.Easy.Core.Attributes;
using IM.Easy.Entity.System;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using Microsoft.AspNetCore.Mvc;

namespace IM.Easy.Api.Controllers.System
{
    [Route("system/menu")]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;
        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }

        /// <summary>
        /// 获取菜单数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="leafNode"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Api(Name = "获取菜单详情", Perm = "menu:info")]
        public SysMenu Get([FromRoute] long id, [FromQuery] bool leafNode)
        {
            var parm = new MenuInfoQueryDto { Id = id, LeafNode = leafNode };
            return _menuService.GetMenu(parm);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [Api(Name = "添加菜单", Perm = "menu:add")]
        public SysMenu Add([FromBody] SysMenu menu)
        {
            _menuService.AddMenu(menu);
            return menu;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="menu"></param>
        [Api(Name = "修改菜单", Perm = "menu:put")]
        public bool Update([FromBody] SysMenu menu)
        {
            return _menuService.UpdateMenu(menu);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [Api(Name = "删除菜单", Perm = "menu:del")]
        public bool Delete([FromRoute] long id)
        {
            return _menuService.DeleteMenu(id);
        }

        [Api(Name = "获取菜单列表")]
        public List<SysMenu> GetList([FromQuery] MenuQueryDto query)
        {
            var menus = _menuService.GetMenuList(query);
            return menus;
        }

        /// <summary>
        /// 获取菜单树
        /// </summary>
        [Api(Name = "获取菜单树", Perm = "menu:tree")]
        public List<SysMenu> GetTree()
        {
            var tree = _menuService.GetMenuTree();
            return tree;
        }


    }
}
